﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using AccountProxy = ChatForum.Client.AccountServiceProxy;
using ForumProxy = ChatForum.Client.ForumServiceProxy;

namespace ChatForum.Client
{
    public class MyPostView
    {
        public string Description { get; private set; }
        public string CreateDate { get; private set; }
        public string CategoryName { get; private set; }
        public Guid PostId { get; private set; }
        public string PostCreatedBy { get; private set; }
        public string PostShortDescription { get; private set; }
        public ForumProxy.Post PostEntity { get; private set; }
        public bool CreatorOnline { get; private set; }
        public bool OnlineImageVisible
        {
            get
            {
                return PostCreatedBy != HttpContext.Current.User.Identity.Name;
            }
        }
        public MyPostView(ForumProxy.Post post)
        {
            PostEntity = post;
            Description = post.Description;
            CreateDate = post.CreatedDate.ToString("F");
            CategoryName = ServiceHelper.Instance.ForumService.GetCategoryById(post.ParentCategory).Name;
            PostId = post.ID;
            PostCreatedBy = post.CreatedBy;
            PostShortDescription = Description.Length > 100 ? Description.Substring(0, 100) + "..." : Description;
            CreatorOnline = ServiceHelper.Instance.AccountService.GetUser(PostCreatedBy).IsOnline;
        }
        private int level;
        public int Level
        {
            get
            {
                if (level != 0)
                    return level;
                if (PostEntity.ParentPost == Guid.Empty)
                    level = 0;
                else
                {
                    MyPostView parent = new MyPostView(ServiceHelper.Instance.ForumService.GetPostById(PostEntity.ParentPost));
                    level = parent.Level + 1;
                }
                return level;
            }
        }
        public int Width { get { return Math.Max(40, 100 - Level * 10); } }
    }
}